package lang.test;


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 *
 *
 * @author：Melody_LQ
 * @time：2022/8/9 12:55 上午
 */
public class MyClassLoader extends ClassLoader{

    protected Class<?> findClass(String name) {
        byte [] bytes = null;
            try {
                bytes = getBytes(name);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return defineClass(null,bytes,0, bytes.length);
    }

    private byte[] getBytes(String name) throws IOException {
        FileInputStream in = new FileInputStream(name);
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int len = 0;
        while ((len = in.read(bytes))!= -1){
            out.write(bytes,0,len);
        }
        out.close();
        bytes = out.toByteArray();
        return bytes;
    }
}
